package cn.stylefeng.guns.modular.comm.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.comm.util.CommConstant;
import cn.stylefeng.guns.comm.util.DictTypeConstant;
import cn.stylefeng.guns.modular.comm.entity.News;
import cn.stylefeng.guns.modular.comm.entity.Swiper;
import cn.stylefeng.guns.modular.comm.model.params.SwiperParam;
import cn.stylefeng.guns.modular.comm.model.result.SwiperResult;
import cn.stylefeng.guns.modular.comm.service.NewsService;
import cn.stylefeng.guns.modular.comm.service.SwiperService;
import cn.stylefeng.guns.sys.core.properties.GunsProperties;
import cn.stylefeng.guns.sys.modular.system.entity.Dict;
import cn.stylefeng.guns.sys.modular.system.entity.FileInfo;
import cn.stylefeng.guns.sys.modular.system.service.DictService;
import cn.stylefeng.guns.sys.modular.system.service.FileInfoService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 轮播图控制器
 *
 * @author
 * @Date 2019-11-18 13:24:11
 */
@Controller
@RequestMapping("/swiper")
public class SwiperController extends BaseController {

    private String PREFIX = "/comm/swiper";

    @Autowired
    private SwiperService swiperService;
    @Autowired
    private FileInfoService fileInfoService;
    @Autowired
    private GunsProperties gunsProperties;
    @Autowired
    private DictService dictService;
    @Autowired
    private NewsService newsService;

    /**
     * 跳转到主页面
     *
     * @author
     * @Date 2019-11-18
     */
    @RequestMapping("")
    public String index() {
        List<Dict> mklx = dictService.listDictsByCode(DictTypeConstant.MKLX);
        setAttr("mklx", mklx);
        return PREFIX + "/swiper.html";
    }

    /**
     * 新增页面
     *
     * @author
     * @Date 2019-11-18
     */
    @RequestMapping("/add")
    public String add() {
        List<Dict> mklx = dictService.listDictsByCode(DictTypeConstant.MKLX);
        setAttr("mklx", mklx);

        List<News> displayNews = newsService.findDisplayNews();
        setAttr("displayNews", displayNews);
        return PREFIX + "/swiper_add.html";
    }

    /**
     * 编辑页面
     *
     * @author
     * @Date 2019-11-18
     */
    @RequestMapping("/edit")
    public String edit() {
        List<Dict> mklx = dictService.listDictsByCode(DictTypeConstant.MKLX);
        setAttr("mklx", mklx);

        List<News> displayNews = newsService.findDisplayNews();
        setAttr("displayNews", displayNews);
        return PREFIX + "/swiper_edit.html";
    }

    /**
     * 新增接口
     *
     * @author
     * @Date 2019-11-18
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(SwiperParam swiperParam) {
        if (ToolUtil.isEmpty(swiperParam.getOpenFlag())) {
            swiperParam.setOpenFlag("1");
        }
        this.swiperService.add(swiperParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author
     * @Date 2019-11-18
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(SwiperParam swiperParam) {
        if (ToolUtil.isEmpty(swiperParam.getOpenFlag())) {
            swiperParam.setOpenFlag("1");
        }
        this.swiperService.update(swiperParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author
     * @Date 2019-11-18
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(SwiperParam swiperParam) {
        this.swiperService.delete(swiperParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author
     * @Date 2019-11-18
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(SwiperParam swiperParam) {
        Swiper detail = this.swiperService.getById(swiperParam.getId());
        SwiperResult swiperResult = new SwiperResult();
        ToolUtil.copyProperties(detail, swiperResult);
        FileInfo fileInfo = fileInfoService.getById(detail.getFileId());
        swiperResult.setFilePath(CommConstant.FILE_PATH + fileInfo.getFilePath().substring(gunsProperties.getFileUploadPath().length() - 1));
        return ResponseData.success(swiperResult);
    }

    /**
     * 查询列表
     *
     * @author
     * @Date 2019-11-18
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(SwiperParam swiperParam) {
        return this.swiperService.findPageBySpec(swiperParam);
    }

}


